package com.ruohi.manage;

import java.sql.Connection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.ruohi.util.dbutil.DBHelper;

/**
 * 数据初始化
 * 
 * @author Administrator
 * 
 */
public class ApplicationListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		// 销毁计划任务的线程
		this.destroyedScheduler();
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		// 加载静态变量，需在所有初始化之前
		loadStaticVariable();
		// 初始化品牌
		// getBrandMapProduct(event.getServletContext());
		//getBrandMapProduct(event.getServletContext());
		getCategoryMapGift(event.getServletContext());
		getBrandMapGift(event.getServletContext());
		getSupplierMapGift(event.getServletContext());
		//getBrandMapPart(event.getServletContext());
		//getCategoryMapPart(event.getServletContext());
		
		//-new
		getEnumListForProduct(event.getServletContext());
		
		String realPath = event.getServletContext().getRealPath("");
		if (realPath.indexOf("/sun21_04/") != -1) {
			loadJob();
		}
	}

	/**
	 * 初始化品牌-产品中心
	 * 
	 * @param application
	 */
	@SuppressWarnings( { "unchecked", "static-access" })
	public static void getBrandMapProduct(ServletContext application) {
		DBHelper dbHelper = DBHelper.getInstance();
		HashMap<String, String> brandMap = null;
		HashMap<String, String> brandUsernMap = null;
		List brandList = null;
		String sql = " select * from pro_agent_factory where flag = 1 order by name asc ";
		Connection conn_pro = null;
		try {
			conn_pro = PoolManager.getConnection(DBType.getDBTypeByEnum("106016"));
			brandList = dbHelper.getMapList(sql, conn_pro);
			brandMap = new HashMap<String, String>();
			brandUsernMap = new HashMap<String, String>();
			Map info = null;
			for (int i = 0; i < brandList.size(); i++) {
				info = (Map) brandList.get(i);
				brandMap.put(info.get("id").toString(), info.get("name").toString());
				brandUsernMap.put(info.get("id").toString(), info.get("usern").toString());
			}
			application.setAttribute("productBrandUsernMap", brandUsernMap);
			application.setAttribute("productBrandMap", brandMap);
			application.setAttribute("productBrandList", brandList);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			dbHelper.freeConnection(conn_pro);
		}
	}

	/**
	 * 
	 * @describe 商品网-商品类别
	 * 
	 */
	@SuppressWarnings( { "unchecked", "static-access" })
	public static void getCategoryMapGift(ServletContext application) {
		DBHelper dbHelper = DBHelper.getInstance();
		HashMap<String, String> categoryMap = null;
		List categoryList = null;
		String sql = " select * from gift_category where is_show = 1 order by order_no desc,no asc ";
		Connection conn = null;
		try {
			conn = PoolManager.getConnection(DBType.getDBTypeByEnum("106013"));
			categoryList = dbHelper.getMapList(sql, conn);
			categoryMap = new HashMap<String, String>();
			Map info = null;
			for (int i = 0; i < categoryList.size(); i++) {
				info = (Map) categoryList.get(i);
				categoryMap.put((String) info.get("no"), (String) info.get("name"));
			}
			application.setAttribute("giftCategoryMap", categoryMap);
			application.setAttribute("giftCategoryList", categoryList);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			dbHelper.freeConnection(conn);
		}
	}

	/**
	 * 初始化品牌-商品网
	 * 
	 * @param application
	 */
	@SuppressWarnings( { "unchecked", "static-access" })
	public static void getBrandMapGift(ServletContext application) {
		DBHelper dbHelper = DBHelper.getInstance();
		HashMap<String, String> brandMap = null;
		HashMap<String, String> brandAgentDiscountMap = null;
		HashMap<String, String> brandStaffDiscountMap = null;
		List brandList = null;
		String sql = " select * from gift_brand where is_show = 1 order by name asc ";
		Connection conn = null;
		try {
			conn = PoolManager.getConnection(DBType.getDBTypeByEnum("106013"));
			brandList = dbHelper.getMapList(sql, conn);
			brandMap = new HashMap<String, String>();
			brandAgentDiscountMap = new HashMap<String, String>();
			brandStaffDiscountMap = new HashMap<String, String>();
			Map info = null;
			for (int i = 0; brandList != null && brandList.size() > 0 && i < brandList.size(); i++) {
				info = (Map) brandList.get(i);
				brandMap.put(info.get("no").toString(), info.get("name").toString());
				brandAgentDiscountMap.put(info.get("no").toString(), info.get("discount_agent") == null ? "" : info.get("discount_agent").toString());
				brandStaffDiscountMap.put(info.get("no").toString(), info.get("discount_staff") == null ? "" : info.get("discount_staff").toString());
			}
			application.setAttribute("giftBrandAgentDiscountMap", brandAgentDiscountMap);
			application.setAttribute("giftBrandStaffDiscountMap", brandStaffDiscountMap);
			application.setAttribute("giftBrandMap", brandMap);
			application.setAttribute("giftBrandList", brandList);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			dbHelper.freeConnection(conn);
		}
	}

	/**
	 * 初始化供应商-商品网
	 * 
	 * @param application
	 */
	@SuppressWarnings( { "unchecked", "static-access" })
	public static void getSupplierMapGift(ServletContext application) {
		DBHelper dbHelper = DBHelper.getInstance();
		HashMap<String, String> supplierMap = null;
		List supplierList = null;
		String sql = " select * from gift_supplier where is_show = 1 order by name asc ";
		Connection conn = null;
		try {
			conn = PoolManager.getConnection(DBType.getDBTypeByEnum("106013"));
			supplierList = dbHelper.getMapList(sql, conn);
			supplierMap = new HashMap<String, String>();
			Map info = null;
			for (int i = 0; supplierList != null && supplierList.size() > 0 && i < supplierList.size(); i++) {
				info = (Map) supplierList.get(i);
				supplierMap.put(info.get("no").toString(), info.get("name").toString());
			}
			application.setAttribute("giftSupplierMap", supplierMap);
			application.setAttribute("giftSupplierList", supplierList);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			dbHelper.freeConnection(conn);
		}
	}

	

	
	/**
	 * 
	 * @describe 商品网-商品类别
	 * @author gp
	 */
	@SuppressWarnings( { "unchecked", "static-access" })
	public static void getEnumListForProduct(ServletContext application) {
		DBHelper dbHelper = DBHelper.getInstance();
	    LinkedHashMap enumMap = null;
	    List enumList = null;
		String sql = " select * from service_common_enum_product where is_show = 1 order by order_no desc,no asc ";
		Connection conn = null;
		try {
			conn = dbHelper.getConnection();
			enumList = dbHelper.getMapList(sql, conn);
			enumMap = new LinkedHashMap();
			Map info = null;
			for (int i = 0; i < enumList.size(); i++) {
				info = (Map)enumList.get(i);
				enumMap.put((String)info.get("no"), (String)info.get("name"));
			}
			application.setAttribute("enumProductMap", enumMap);
			application.setAttribute("enumProductList", enumList);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			dbHelper.freeConnection(conn);
		}
	}

	private void destroyedScheduler() {
		try {
			JobExecute jobExecute = new JobExecute();
			jobExecute.shutdown();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 加载静态变量的值
	 */
	public void loadStaticVariable() {
		Tools.loadStaticVariable();
	}

	public void loadJob() {
		try {
			JobExecute jobExecute = new JobExecute();
			jobExecute.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
